package com.ysxsoft.wdg.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.umeng.socialize.UMShareAPI;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.order.PinOrderDetailActivity;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.AddressBean;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.response.PinOrderResponse;
import com.ysxsoft.wdg.bean.zy.ZyOrderDetailBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.fragment.THuiWebFragment;
import com.ysxsoft.wdg.util.JavaInterface;
import com.ysxsoft.wdg.util.ObserverMap;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.WebViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.SharePopupWindow;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * webViw基类
 */
public class KanWebViewActivity extends BaseActivity implements ObserverMap.IPageDataChangeObserver {

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.parent)
    LinearLayout parent;
    private SharePopupWindow popupWindow;
    private UMShareAPI mShareAPI;

    public static void start(Context context, String url) {
        Intent i = new Intent(context, KanWebViewActivity.class);
        i.putExtra("url", url);
        context.startActivity(i);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_kan_web_view;
    }

    @Override
    protected void initData() {
        String url = getIntent().getStringExtra("url");
        ObserverMap.reg("KanWebViewActivity", this);
        initStatusBar(topView, false);

        WebViewUtils.init(webView);
        webView.setWebViewClient(new WebViewC());
        webView.addJavascriptInterface(new JavaInterface(), "aa");
        webView.loadUrl(url);

        mShareAPI = UMShareAPI.get(KanWebViewActivity.this);
        popupWindow = new SharePopupWindow(KanWebViewActivity.this, mShareAPI);
    }

    @Override
    protected void setListener() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ObserverMap.remove("KanWebViewActivity");
    }

    @Override
    public void change() {

    }

    @Override
    public void share(String gid, String url) {
        //我的砍价分享
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                popupWindow.setShareData(getString(R.string.app_name), url, url, "", 0);
                popupWindow.show(parent);
            }
        });
    }

    /**
     * 拼团详情里边的邀请
     * @param gid
     * @param url
     */
    @Override
    public void share2(String gid, String url) {
        //拼团中  拼团成功 订单详情邀请
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                popupWindow.setShareData(getString(R.string.app_name), url, url, "", 0);
                popupWindow.show(parent);
            }
        });
    }

    @Override
    public void pay(String orderId, String totalMoney, int type, String url) {
    }

    @Override
    public void back() {
        //我的砍价分享
        Log.e("tag","返回");
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
    }

    @Override
    public void toPinOrderDetail(String orderid) {
        //跳转拼团订单详情
        mApiHelper.getZyOrderDetail(SharePrefUtils.getUserId(), orderid, new Observer<BaseBean<ZyOrderDetailBean>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<ZyOrderDetailBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    ZyOrderDetailBean data = baseBean.getData();
                    //新实体类
//                    PinOrderResponse.DataBean dataBean = new PinOrderResponse.DataBean();
//                    dataBean.setAddtime(data.getScore());
                    Intent intent = new Intent(KanWebViewActivity.this, PinOrderDetailActivity.class);
                    intent.putExtra("orderid",data.getOrder().getOrderid());
                    startActivity(intent);
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public void toKanOrderDetail(String id) {
        //跳转砍价订单详情
        Intent intent = new Intent(KanWebViewActivity.this, PinOrderDetailActivity.class);
        intent.putExtra("orderid",id);
        startActivity(intent);
    }

    private class WebViewC extends WebViewClient {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
            super.onReceivedSslError(view, handler, error);
        }
    }
}
